require('../css/login.css');

//导入sxios
const axios = require('../utils/http.js');
//导入local
const local = require('../utils/local.js');


window.addEventListener('load', function () {

    let btn = this.document.querySelector('.btn');
    let pwd_inp = this.document.querySelector('.pwd_inp');
    let pwd2_inp = this.document.querySelector('.pwd2_inp');
    //跳转注册页
    let pwd_login = document.querySelector('.pwd_login');
    pwd_login.addEventListener('click', function () {
        location.href = './register.html';
    })

    btn.addEventListener('click', () => {
        //非空验证通过便请求axios
        if (!pwd_inp.value || !pwd2_inp.value) {
            // this.alert('请输入账号密码')
            utils.toast(1, '请输入账号密码')
            return;
        } else {
            data = {
                account: pwd_inp.value,
                password: pwd2_inp.value,
            }
            axios.post('/api/user/login', data).then(function (res) {
                console.log(res);

                if (res.data.errno == 0) {

                    utils.toast(0, '登录成功')
                   //获取token
                    let token = res.data.data.token;
                    //将后端返回的令牌存储到本地
                    local.set('token',token);
                    //跳转页面
                    location.href = './index.html';
                } else {

                    utils.toast(1, res.data.message)
                }
            })
        }

    })








})

